import type { ProFormInstance } from '@ant-design/pro-components';
import {
  DrawerForm,
  ProForm,
  ProFormDatePicker,
  ProFormDigit,
  ProFormRadio,
  ProFormSelect,
  ProFormSwitch,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-components';
import { useRequest } from '@umijs/max';
import React, { useRef, useState } from 'react';
import { useModel } from 'umi';
import type { FormRowData } from '../data';
import { getDomainList, getSites } from '../service';
import Password from 'antd/lib/input/Password';
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<FormRowData>;

export type CreateUserProps = {
  onCancel: (flag?: boolean, formVals?: FormValueType) => void;
  onSubmit: (values: FormValueType) => Promise<void>;
  updateModalVisible: boolean;
  values: Partial<FormRowData>;
  formType: number;
};
const superRole ={  'normal': '普通渠道'}
const userRole= {
   'super': '超管',
    'admin': '管理',
    'normal': '普通渠道'
}
const cpsConf = [
  {
    label: 'APP_TEST',
    value: 'CPS-ZBJ-yf',
  },
  {
    label: 'APP',
    value: 'CPS-ZBJ-yf2',
  },
  {
    label: 'APP_3',
    value: 'CPS-ZBJ-yf3',
  },
  {
    label: 'APP_4',
    value: 'CPS-ZBJ-yf4',
  },
  {
    label: 'APP_5',
    value: 'CPS-ZBJ-yf5',
  },
  {
    label: 'APP_6',
    value: 'CPS-ZBJ-yf6',
  },
  {
    label: 'APP_7',
    value: 'CPS-ZBJ-yf7',
  },
];
// eslint-disable-next-line react-hooks/rules-of-hooks
const CreateUserDrawer: React.FC<CreateUserProps> = (props) => {
  const formRef = useRef<ProFormInstance>();
  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState || {};
  const [requestStatus] = useState(currentUser?.userInfo?.role==='super')
  return (
    <DrawerForm
      title={props?.formType===0?"新建用户":"编辑用户"}
      width="820px"
      formRef={formRef}
      visible={props.updateModalVisible}
      onVisibleChange={(val) => {
        if (!val) {
          props.onCancel();
        }
        formRef?.current?.setFieldsValue({
          nickname: props?.values.nickname,
          role: props?.values.role,
          status: props?.values.status,
          mobile: props?.values.mobile,
          password: props?.values.password,
          wechat: props?.values.wechat,
          zfbName: props?.values.zfbName,
          zfbAcount: props?.values.zfbAcount,
          bankName: props?.values.bankName,
          subBankName: props?.values.subBankName,
          bankUserName: props?.values.bankUserName,
          bankNum: props?.values.bankNum,
        });
      }}
      onFinish={async (val) => {
        console.log(val);
        let params = val;
        let status = ~~val.status;
        if(props?.formType ===1) {
          params = {...params,id: props?.values.id, status}
        }
        props.onSubmit({...params, status});
      }}
    >
      <ProFormText
        width="md"
        name="nickname"
        label="用户名"
        tooltip="最长为 24 位"
        placeholder="请输入用户名"
        rules={[{ required: true, message: '用户名不能为空!' }]}
      />
      <ProForm.Group>
      <ProFormSelect
        colProps={{  md: 12 }}
        label="角色选择"
        name="role"
        rules={[{ required: true, message: '角色必选!' }]}
        valueEnum={
          requestStatus ? userRole : superRole
        }
      />
      <ProFormSwitch
        checkedChildren="正在使用"
        unCheckedChildren="停止使用"
        label="用户状态"
        name="status"
      
      />
     {requestStatus ? <ProFormSelect
        colProps={{  md: 12 }}
        label="口令渠道"
        name="extData"
        request={async()=>{
          return cpsConf
        }}
      />: ''}
      </ProForm.Group>
      <ProForm.Group>
      
      {
        props?.formType === 0 ? <ProFormText
        width="sm"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        rules={[{ required: true, message: '手机号不能为空!' }]}
      />: ''
      }
     {
      props?.formType === 0 ? <ProFormText
      width="sm"
      name="password"
      label="密码"
      placeholder="请输入密码"
      rules={[{ required: true, message: '密码不能为空!' }]}
    />: ''
     }
      </ProForm.Group>
      <ProForm.Group>
      <ProFormText
        width="sm"
        name="wechat"
        label="微信号"
        placeholder="请输入微信号"
      />
      <ProFormText
        width="sm"
        name="zfbName"
        label="支付宝姓名"
        placeholder="请输入支付宝姓名"
      />
      <ProFormText
        width="sm"
        name="zfbAcount"
        label="支付宝账号"
        placeholder="请输入支付宝账号"
      />
      </ProForm.Group>
      <ProForm.Group>
      <ProFormText
        width="md"
        name="bankName"
        label="开户行名称"
        placeholder="请输入开户行名称"
      />
       <ProFormText
        width="md"
        name="subBankName"
        label="支行名称"
        placeholder="请输入支行名称"
      />
       <ProFormText
        width="md"
        name="bankUserName"
        label="持卡人名字"
        placeholder="请输入持卡人名字"
      />
      <ProFormText
        width="md"
        name="bankNum"
        label="开户行账号"
        placeholder="请输入开户行账号"
      />
      </ProForm.Group>
     
    </DrawerForm>
  );
};

export default CreateUserDrawer;
